package com.sunan.code;


import com.apple.eawt.Application;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CodeTools {
    private JPanel rootPanel;
    private JTextArea textArea1;
    private JTextArea textArea2;
    private JTextArea textArea3;
    private JButton buildBtn;
    private JTextArea resultTextArea;
    private JTextArea templateTextArea;
    private JCheckBox campCheckBox;
    private JButton delBrackets;

    private String line = System.lineSeparator();
    private Map<String, String> mysqlMapJava = new HashMap<>();

    public CodeTools() {
        //初始化模板
        StringBuilder sb = new StringBuilder();
        sb.append("/**");
        sb.append(line);
        sb.append(" * @Z");
        sb.append(line);
        sb.append(" */");
        sb.append(line);
        sb.append("private @Y @X;");
        this.templateTextArea.setText(sb.toString());

        //初始化所需数据
        mysqlMapJava.put("varchar", "String");
        mysqlMapJava.put("char", "String");
        mysqlMapJava.put("text", "String");
        mysqlMapJava.put("mediumtext", "String");
        mysqlMapJava.put("tinytext", "String");
        mysqlMapJava.put("longtext", "String");
        mysqlMapJava.put("int", "Integer");
        mysqlMapJava.put("date", "Date");
        mysqlMapJava.put("datetime", "Date");
        mysqlMapJava.put("decimal", "BigDecimal");
        mysqlMapJava.put("bigint", "Long");
        mysqlMapJava.put("bit", "Boolean");
        mysqlMapJava.put("double", "Double");
        mysqlMapJava.put("float", "Float");


        //各种事件
        buildBtn.addActionListener(e -> {
            clickBuildBtn();
        });
        delBrackets.addActionListener(e -> {
            clickDelBracketsBtn();
        });
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("CodeTools");
        frame.setLocationRelativeTo(null);//窗体居中
        BufferedImage image = null;
        try {
            image = ImageIO.read(frame.getClass()
                    .getResource("/icon.gif"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        frame.setIconImage(image);//windows下icon样式
        if (System.getProperty("os.name").toLowerCase(Locale.ENGLISH).startsWith("mac")) {
            Application.getApplication().setDockIconImage(image);//mac改变程序坞的图标样式
        }

//        frame.setSize(400, 500);
        frame.setContentPane(new CodeTools().rootPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    /**
     * 点击生成按钮
     */
    private void clickBuildBtn() {
        StringBuilder resultSb = new StringBuilder();
        String templateWord = this.templateTextArea.getText();
        int index = 0;
        for (String word : this.textArea1.getText().split(line)) {
            String[] wordYArr = this.textArea2.getText().split(line);
            String wordY = wordYArr.length > index ? wordYArr[index] : "";
            String[] wordZArr = this.textArea3.getText().split(line);
            String wordZ = wordZArr.length > index ? wordZArr[index] : "";
            String wordX = word;
            if (this.campCheckBox.isSelected()) {
                wordX = campName(wordX);
            }
            if (mysqlMapJava.containsKey(wordY)) {
                wordY = mysqlMapJava.get(wordY);
            }
            if (!word.equals("")) {
                resultSb.append(templateWord.replaceAll("@X", wordX).replaceAll("@Y", wordY).replaceAll("@Z", wordZ));
            }
            index++;
        }
        this.resultTextArea.setText(resultSb.toString());
    }

    /**
     * 点击去掉括号
     */
    private void clickDelBracketsBtn() {
        StringBuilder resultSb = new StringBuilder();
        Pattern pattern = Pattern.compile("(.*)(\\(.*\\))");
        int index = 1;
        String[] arr = this.textArea2.getText().split(line);
        for (String word : arr) {
            Matcher m = pattern.matcher(word);
            if (m.find()) {
                resultSb.append(m.group(1));
            } else {
                resultSb.append(word);
            }
            if (arr.length != index) {
                resultSb.append(line);
            }
            index++;
        }
        this.textArea2.setText(resultSb.toString());
    }

    /**
     * 下划线改驼峰命名
     *
     * @param word
     * @return
     */
    private String campName(String word) {
        String resultWord = "";
        if (!word.equals("")) {
            String[] wordArr = word.split("_");
            if (wordArr.length > 1) {
                for (int i = 0; i < wordArr.length; i++) {
                    if (i > 0) {
                        resultWord += toUpperCaseFirstOne(wordArr[i]);
                    } else {
                        resultWord += wordArr[i];
                    }
                }
            } else {
                resultWord = word;
            }

        }
        return resultWord;
    }

    /**
     * 首字母大写
     *
     * @param s
     * @return
     */
    private String toUpperCaseFirstOne(String s) {
        if (Character.isUpperCase(s.charAt(0)))
            return s;
        else
            return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
    }

    private void initData() {

    }

}
